<?php


/*
 *
 * Moze dodawac/edytowac/usuwac tylko wykladowcow!
 *
 */
class Technical_AccountController extends Zend_Controller_Action
{

	public function init()
	{
		/* Initialize action controller here */
	}

	public function indexAction()
	{
		$session = new Zend_Session_Namespace('identity');
		$users = new Application_Model_Dao_User();
		if ($this->_getParam('lecturer') == 'true') {
			$result = $users->getAllLecturer();
			$this->view->head = "Wykładowcy";
			$this->view->users = $result;
		} else {
			$result = $users->getAllUser();
			$this->view->head = "Użytkownicy";
			$this->view->users = $result;
		}
		if($result == null){
			$this->view->empty = "Brak użytkowników w bazie.";
		}else{
			$this->view->empty = "";
		}
		$designation = $this->_getParam('designation');
		if(isset($session->statusdel) && $this->_getParam('statusdelete') == 'true') {
			$this->view->delete = "Rekord \"".$designation."\" został poprawnie usunięty.";
			unset($session->statusdel);
		}else{
			$this->view->delete = "";
		}

		if( isset( $session->attention ) ) {
			$this->view->attention = $session->attention ;
			unset( $session->attention );
		} else {
			$this->view->attention = "";
		}

		if( isset( $session->updatesuccess ) ) {
			$this->view->updateStatus = $session->updatesuccess ;
			unset( $session->updatesuccess );
			if(isset( $session->updaterecorddesignation ) ) {
				$this->view->updateRecordDesignation = $session->updaterecorddesignation;
				unset( $session->updaterecorddesignation );
			} else {
				$this->view->updateRecordDesignation = "";
			}
		} else if( isset($session->deletesuccess) ) {
			$this->view->deleteStatus = $session->deletesuccess;
			unset($session->deletesuccess);
			if(isset( $session->deleterecorddesignation ) ) {
				$this->view->deleteRecordDesignation = $session->deleterecorddesignation;
				unset( $session->deleterecorddesignation );
			} else {
				$this->view->deleteRecordDesignation = "";
			}
		} else if( isset($session->addsuccess) ) {
			$this->view->addStatus = $session->addsuccess;
			unset($session->addsuccess);
			if(isset( $session->addrecorddesignation ) ) {
				$this->view->addRecordDesignation = $session->addrecorddesignation;
				unset( $session->addrecorddesignation );
			} else {
				$this->view->addRecordDesignation = "";
			}
		} else {
			$this->view->deleteRecord = "";
		}
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_Account();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		// edycja oraz dodawanie w jquery, kod obsługujący requesta z danymi z formularza, zapis do bazy
		if( $this->getRequest()->isPost() ) {
			$formData = $this->getRequest()->getPost();
			if( $form->isValid($formData) ) {
				$user = new Application_Model_Entities_User();
				$user->setId($form->getValue('id'));
				$user->setAcademicDegree( $form->getValue('academicDegree_id') );
				$user->setName( $form->getValue('name') );
				$user->setSurname( $form->getValue('surname') );
				$user->setEmail( $form->getValue('email') );
				$user->setPassword( $form->getValue('password') );
				$user->setCathedral( $form->getValue('cathedral_id') );
				$user->setRole( $form->getValue('role_id') );
				$user->setLecturer( $form->getValue('lecturer') );
				$userDao = new Application_Model_Dao_User();
				if($form->getValue('typeForm') == 'editform' ) {
					//print_r($coursesType);
					$result = $userDao->updateUser($user);
					if($result > 0 ) {
						$session->updatesuccess = 'updatesuccess';
					} else {
						$session->updatesuccess = 'updatefail';
					}
				} else if ($form->getValue('typeForm') == 'addform' ) {
					$result = $userDao->saveUser($user);
					if($result > 0 ) {
						$session->addsuccess = 'addsuccess';
						$session->addrecorddesignation = $form->getValue('name')." ".$form->getValue('surname');
					} else {
						$session->addsuccess = 'addfail';
					}
				} else {
					$session->attention = 'Błędne dane dotyczące typu operacji';
				}
				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		} else {
			$session->attention = '';
		}

	}

	public function addAction()
	{
		$session = new Zend_Session_Namespace('identity');
		$form = new Application_Form_Account();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		$form->getElement('typeForm')->setValue('addform');
		$form->submit->setLabel('Dodaj nowego użytkownika');
		$request = $this->getRequest();
		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$user = new Application_Model_Entities_User();
				$user->setAcademicDegree( $form->getValue('academicDegree_id') );
				$user->setName( $form->getValue('name') );
				$user->setSurname( $form->getValue('surname') );
				$user->setEmail( $form->getValue('email') );
				$user->setPassword( $form->getValue('password') );
				$user->setCathedral( $form->getValue('cathedral_id') );
				$user->setRole( $form->getValue('role_id') );
				$user->setLecturer( $form->getValue('lecturer') );
				$userDao = new Application_Model_Dao_User();
				$userDao->saveUser($user);
				//	$result = Zend_Auth::getInstance()->authenticate($user);
				//if ($result->isValid()) {
				$this->_helper->redirector('index');
				//} else {
				//$form->login->addError('Należy wypełnić wszystkie pola.');
				//}
				} else {
					$session->attention = 'Przesłane dane w formularzu nie są poprawne.';
					$this->_helper->redirector( 'index');
				}
			} else {
				$session->attention = 'Formularz prawdopodobnie nie został wysłany.';
				//$this->_helper->redirector( 'index');
			}
			$this->view->AccountForm = $form;
		}

		public function editAction()
		{
			$session = new Zend_Session_Namespace('identity');
			$form = new Application_Form_Account();
			$form->appendValidationEngineFiles($this->view);
			$polish = BH_TranslatePL::getPolishTranslation();
			$translate = new Zend_Translate('array', $polish, 'pl');
			$form->setTranslator($translate);
			$form->submit->setLabel('Zatwierdź zmiany');
			$this->view->AccountEditForm = $form;
			if ($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();
				if ($form->isValid($formData)) {
					$user = new Application_Model_Entities_User();
					$user->setId($form->getValue('id'));
					$user->setAcademicDegree( $form->getValue('academicDegree_id') );
					$user->setName( $form->getValue('name') );
					$user->setSurname( $form->getValue('surname') );
					$user->setEmail( $form->getValue('email') );
					$user->setPassword( $form->getValue('password') );
					$user->setCathedral( $form->getValue('cathedral_id') );
					$user->setRole( $form->getValue('role_id') );
					$user->setLecturer( $form->getValue('lecturer') );
					$userDao = new Application_Model_Dao_User();
					$userDao->updateUser($user);
					$this->_helper->redirector('index');
				} else
				$form->populate($formData);
			} else {
				$id = $this->_getParam('id', 0);
				if ($id > 0) {
					$userDao = new Application_Model_Dao_User();
					$form->populate($userDao->getUserByID($id));
					$session->updaterecorddesignation = $form->getValue('name')." ".$form->getValue('name');
					$form->getElement('typeForm')->setValue('editform');
				} else {
					$session->attention = 'Błąd podczas pobierania danych lub podczas przypisywania ich do pól formularza.';
					$this->_helper->redirector( 'index');
				}
			}
		}

		public function deleteAction(){
			$session = new Zend_Session_Namespace('identity');
			$request = $this->getRequest();
			if ($request->isPost()) {
				$del = $request->getPost('del');
				if ($del == 'Tak, usuń rekord') {
					$id = $request->getPost('id');
					$designation = $request->getPost('designation');
					$user = new Application_Model_Entities_User();
					$user->setId($id);
					$userDao = new Application_Model_Dao_User();
					$userDao->deleteUser($user);
					$session = new Zend_Session_Namespace('identity');
					$session->statusdel = 'true';
					$this->_helper->redirector( 'index', 'account', 'technical', array('statusdelete' => 'true', 'designation' => $designation));
				}else
				$this->_helper->redirector( 'index');
					
			} else if($this->_getParam('jsflag') == 'false' ){
				$id = $this->_getParam('id', 0);
				if ($id > 0) {
					$userDao = new Application_Model_Dao_User();
					$result = $userDao->getRowUser($id);
					$this->view->users = $result;
				}
			} else if($this->_getParam('jsflag') == 'true' ){
				$id = $this->_getParam('id', 0);
				$user = new Application_Model_Entities_User();
				$user->setId($id);
				$userDao = new Application_Model_Dao_User();
				$result = $userDao->deleteUser($user);
				if($result > 0) {
					$session->deletesuccess = "deletesuccess" ;
					$session->deleterecorddesignation = $this->_getParam('name', 0);
				} else {
					$session->deleterecorddesignation = "";
					$session->deletesuccess = "deletefail" ;
				}
			} else {
				$this->_helper->redirector('index');
			}
		}

	}



